package com.zjpavt.client.udp.udpservice;

import com.zjpavt.client.event.EventRegisterCenter;
import com.zjpavt.client.hal.DeviceID;
import com.zjpavt.client.hal.network.NetworkManager;
import com.zjpavt.client.udp.UdpSender;
import io.netty.channel.ChannelHandlerContext;

/**
 * 深圳大厅展示
 * 指定时间触发向指定端口发送广播
 * @author zyc
 */
public class ShenZhenDisplay implements UdpService {


    private static final int PORT =20192;
    public static final String STATUS =EventRegisterCenter.STATUS;
    public static final String KEY_NAME = EventRegisterCenter.KEY_NAME;
    public static final String DEVICE_NAME = EventRegisterCenter.DEVICE_NAME;
    public static final String EXT_NUM = EventRegisterCenter.EXT_NUM;

    @Override
    public void execute(byte[] commandBytes) {

    }

    @Override
    public void execute(String command) {

    }

    @Override
    public void execute(String command, ChannelHandlerContext channel) {

    }

    public static void init() {
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DOOR_STATUS_CHANGE,(map)-> {
            if (map == null || map.get(STATUS) == null) {
                return;
            } else {
                onDoorStatusChange((String)map.get(STATUS));
            }
        });

        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.KEY_PRESS,(map)-> {
            if (map == null || map.get(KEY_NAME) == null) {
                return;
            } else {
                onKeypress((int)map.get(KEY_NAME));
            }
        });
        EventRegisterCenter.getInstance().registerTask(EventRegisterCenter.DEVICE_DISCOVER,(map)-> {
            if (map == null || map.get(DEVICE_NAME) == null) {
                return;
            } else {
                onDiscoverDevice((int)map.get(EXT_NUM),(String)map.get(DEVICE_NAME));
            }
        });
    }


    public static void onDiscoverDevice(int extNum, String deviceName) {
//        UdpSender.sendUdpPacket();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("discoverDevice ")
        .append("id=" + DeviceID.getINSTANCE().getDeviceIdCached())
        .append(" ")
        .append("extNum=" + extNum)
        .append(" ")
        .append("deviceName=" + deviceName);
        UdpSender.sendUdpPacket(getEth0BroadcastAddress(), PORT, stringBuilder.toString().getBytes());
    }

    /**
     *
     * @param status  open or close
     */
    public static void onDoorStatusChange(String status) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("doorStatusChange ")
        .append("id=" + DeviceID.getINSTANCE().getDeviceIdCached())
        .append(" ")
        .append("doorStatus=" + status);
        UdpSender.sendUdpPacket(getEth0BroadcastAddress(), PORT, stringBuilder.toString().getBytes());
    }

    public static void onKeypress(int keyName) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("keypress ")
        .append("id=" + DeviceID.getINSTANCE().getDeviceIdCached())
        .append(" ")
        .append("keyName=" + keyName);
        UdpSender.sendUdpPacket(getEth0BroadcastAddress(), PORT, stringBuilder.toString().getBytes());
    }

    /**
     * 获取目标的地址
     * @return
     */
    public static String getEth0BroadcastAddress() {
        String destHostIp = NetworkManager.getInstance().getNetworkMap().get(NetworkManager.INTERFACE_ETH_0).getInterfaceAddress().getBroadcast().getHostAddress();
        return destHostIp;
    }
}
